package edu.ccut.saturn.basecomponent.baseservicecomponent;

import java.io.ByteArrayOutputStream;
import java.io.FileInputStream;

import edu.ccut.saturn.basecomponent.databasecomponent.Messages;
import edu.ccut.saturn.component.DefaultComponent;
import edu.ccut.saturn.component.IParameterContext;

/**
 * 提供文件路径，将文件转换为字节流
 * 
 * @author tony
 * 
 */
public class GetByteArrFromFile extends DefaultComponent {

	@Override
	public void execute(IParameterContext parameterContext) throws Exception {
		debug(Messages.getString("GetByteArrFromFile.0"), parameterContext); //$NON-NLS-1$
		super.execute(parameterContext);
		String path = (String) parameterContext.getParameterValue(0);
		debug("filePath=====" + path, parameterContext);
		if (path != null) {
			FileInputStream fis = new FileInputStream(path);
			byte[] buffer = new byte[1024];
			ByteArrayOutputStream baos = new ByteArrayOutputStream();
			int read ;
			while((read=fis.read(buffer))!=-1){
				baos.write(buffer,0,read);
				baos.flush();
			}
			byte[] bytes = baos.toByteArray();
			baos.close();
			fis.close();
//			fis.read(bytes);
			parameterContext.setParameter(1, bytes);
			debug(Messages.getString("GetByteArrFromFile.1"), parameterContext); //$NON-NLS-1$
		}
	}
}
